import {GPS} from "@/utlis/uni_api";

/**
 *@全局公共配置
 */
export default (proxy: any) => {
    // 消息提示
    const Toast = (
        message: string = '',
        type: string = '',
        icon: string | boolean = '',
        duration: number = 1000
    ) => {
        switch (type) {
            case 'success':
                icon = 'https://cdn.uviewui.com/uview/demo/toast/success.png'
                break
            case 'error':
                icon = 'https://cdn.uviewui.com/uview/demo/toast/error.png'
                break
            case 'warning':
                icon = 'https://cdn.uviewui.com/uview/demo/toast/top.png'
                break
            default:
                icon ? icon : false
        }
        return proxy.$refs.uToast.show({
            type,
            message,
            icon,
            duration
        })
    }

    /**
     * @定位坐标系转换
     * @resolve { Object } 返回经纬度
     * @reject { msg|Object } 返回错误信息
     */
    const CoordinateTransformation = (locations: string, cover: boolean) => {
        return new Promise((resolve, reject) => {
            console.log(locations, "坐标转换地理位置前");
            let arr = locations.split(",");
            let data;
            if (cover) {
                try {
                    let gcj = GPS.gcj_encrypt(Number(arr[1]), Number(arr[0]));
                    console.log(gcj, "坐标转换地理位置后");
                    data = {latitude: gcj.lat, longitude: gcj.lon}
                } catch (error) {
                    data = {latitude: arr[1], longitude: arr[0]}
                }
            }else{
                data = {latitude: arr[1], longitude: arr[0]}
            }
            resolve(data);
        });
        // let data = {
        //   latitude: arr[1],
        //   longitude: arr[0],
        // }
        // return data
        // return new Promise((resolve, reject) => {
        //   const BaseUrl = "https://restapi.amap.com/v3/assistant/coordinate/convert?&coordsys=gps&output=JSON";
        //   const AppKey = "22597b8baa89aa518058e5d7e2bc77f3";
        //   return uni.request({
        //     url: `${BaseUrl}&locations=${locations}&key=${AppKey}`,
        //     data: {},
        //     timeout: 30000,
        //     header: {
        //       Accept: "application/json",
        //       "Content-Type": "application/json",
        //       "X-Requested-With": "XMLHttpRequest",
        //     },
        //     method: "GET",
        //     success: (res: any) => {
        //       console.log(res.data, "坐标转换地理位置");
        //       // 转换成功
        //       if (res.data.status == 1) {
        //         let arr = res.data.locations.split(",");
        //         let data = {
        //           latitude: arr[1],
        //           longitude: arr[0],
        //         }
        //         resolve(data)
        //       }
        //       else {
        //         console.error("坐标转换出错", res.data.info)
        //         reject(res.data.info)
        //       }
        //     },
        //     fail: (error) => {
        //       reject(error)
        //     },
        //   })
        // })
    }
    return {Toast, CoordinateTransformation}
}
